package org.wlw.teacher.utils;

import org.wlw.teacher.anno.Resource;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class DIUtils {

    public static void di(Object runObj) {
        try {
            Class clz = runObj.getClass();
            Method[] ms = clz.getDeclaredMethods();
            for(Method m:ms) {
                if(m.isAnnotationPresent(Resource.class)) {
                    Resource res = m.getAnnotation(Resource.class);
                    String dname = res.value();
                    if("".equals(dname)) {
                        String tname = m.getName();
                        tname = tname.substring(3);//qu set
                        String fname = tname.substring(0,1).toLowerCase();
                        dname = fname+tname.substring(1);
                    }
                    Properties prop = PropertiesUtils.getProperties(Contrants.DI_FILE);
                    String cname = prop.getProperty(dname);
                    Class tclz = Class.forName(cname);
                    Object obj = tclz.newInstance();
                    m.invoke(runObj,obj);
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

}
